﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Cinemax.DTO;
using Cinemax.DAO;

namespace Cinemax.DAO
{
    public class VeDAO
    {
        static rapphimDataContext data = new rapphimDataContext();
        public static List<VeDTO> getAllTicket(int _MaSuatChieu)
        {
            VeDTO ve = new VeDTO();
            SP_GET_TOANBO_VE_THEO_SUATCHIEUResult temp = new SP_GET_TOANBO_VE_THEO_SUATCHIEUResult();
            try
            {
                List<VeDTO> allTicket = new List<VeDTO>();
                List<SP_GET_TOANBO_VE_THEO_SUATCHIEUResult> dsVe = new List<SP_GET_TOANBO_VE_THEO_SUATCHIEUResult>();
                dsVe = data.SP_GET_TOANBO_VE_THEO_SUATCHIEU(_MaSuatChieu).ToList();
                foreach (SP_GET_TOANBO_VE_THEO_SUATCHIEUResult item in dsVe)
                {
                    temp = item;
                    ve = new VeDTO(item);
                    allTicket.Add(ve);
                }
                return allTicket;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public static List<VeDTO> getAllTicketEmpty(int _MaSuatChieu)
        {
            VeDTO ve = new VeDTO();
            try
            {
                List<VeDTO> allTicketEmpty = new List<VeDTO>();
                List<SP_GET_TOANBO_VE_CHUADAT_THEO_SUATCHIEUResult> dsvcd = new List<SP_GET_TOANBO_VE_CHUADAT_THEO_SUATCHIEUResult>();
                dsvcd = data.SP_GET_TOANBO_VE_CHUADAT_THEO_SUATCHIEU(_MaSuatChieu).ToList();
                foreach (SP_GET_TOANBO_VE_CHUADAT_THEO_SUATCHIEUResult item in dsvcd)
                {
                    ve = new VeDTO(item);
                    allTicketEmpty.Add(ve);
                }
                return allTicketEmpty;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public static List<int> getAllIDTicketEmpty(int _MaSuatChieu)
        {
            VeDTO ve = new VeDTO();
            try
            {
                List<int> allTicketEmpty = new List<int>();
                List<SP_GET_TOANBO_VE_CHUADAT_THEO_SUATCHIEUResult> dsvcd = new List<SP_GET_TOANBO_VE_CHUADAT_THEO_SUATCHIEUResult>();
                dsvcd = data.SP_GET_TOANBO_VE_CHUADAT_THEO_SUATCHIEU(_MaSuatChieu).ToList();
                foreach (SP_GET_TOANBO_VE_CHUADAT_THEO_SUATCHIEUResult item in dsvcd)
                {
                    ve = new VeDTO(item);
                    allTicketEmpty.Add(ve.MaVe);
                }
                return allTicketEmpty;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }


        public static VeDTO getVeByID(int id)
        {
            VeDTO v = new VeDTO();
            SP_GET_TICKET_BYIDResult vid = new SP_GET_TICKET_BYIDResult();
            vid = data.SP_GET_TICKET_BYID(id).ElementAt(0);
            v = new VeDTO(vid);
            return v;

        }

        public static int BookTicket(int maVe, int maLoaiVe, int maKhuyenMai, int maTaiKhoan, double thanhTien)
        {
            int result = data.DatVe(maVe, maLoaiVe, maKhuyenMai, maTaiKhoan, thanhTien);
            return result;
        }
        public static int UnBookTicket(int maVe, int maTaiKhoan)
        {
            int result = data.HuyVe(maVe, maTaiKhoan);
            return result;
        }

        public static List<VeDTO> getTicketBookedByID(int id)
        {
            List<VeDTO> dsVe = new List<VeDTO>();
            List<SP_GET_TICKETBOOOKED_BYIDResult> dsVeid = new List<SP_GET_TICKETBOOOKED_BYIDResult>();
            dsVeid = data.SP_GET_TICKETBOOOKED_BYID(id).ToList();
            foreach (SP_GET_TICKETBOOOKED_BYIDResult item in dsVeid)
            {
                VeDTO ve = new VeDTO(item);
                dsVe.Add(ve);
            }
            return dsVe;
        }

        public static double CalcFare(int maLoaiVe, int maKhuyenMai)
        {
            double? result = 0;
            data.TinhTienVe(maLoaiVe, maKhuyenMai, ref result);
            return result.Value;
        }
    }
}